function getNameFromURI(uri){
    //First check if the uri is actually a uri, i.e. whether it starts with "http://"
    if (uri.search(/http/i) > -1){
        var tokens = uri.split("/");
        var lastToken = tokens[tokens.length - 1];
        var hashTokens = lastToken.split("#");
        if (hashTokens.length > 0){
            lastToken = hashTokens[hashTokens.length - 1];
        }
        return lastToken;
    }
    else {
        return uri;
    }
}

function testJSON(){
    var o = new Object();

    var endpoints = 2;
    for (var i = 0; i < endpoints; i++){
        var ep = new Object();
        var endpoint = "Endpoint" + i;
        var properties = 3;
        for (var j = 0; j < properties; j++){
            var property = "p" + j;
            var val = new Object();
            //there's only 1 value per property
            val["value1"] = "AND";
            ep[property] = val;
        }
        o[endpoint] = ep;
    }
	
    var t = JSON.stringify(o);

}

function showProperties(){
    $("#selectors-resizer").hide();
    
    $('form[id=datasources] :checkbox:checked').each(function() {
        if ($('#selectors').find('#' + $(this).val()).length === 0){
            $("#selectors-resizer").show();
            var option = $(this).val();
            var sourceDiv = '<div id="' + option + '">';
            sourceDiv += '<h3><a href="#">' + option + '</a></h3>';
            var sourcename = option + "_prop";
            var sourceid= '#' + sourcename;
            sourceDiv += '<div id="' + sourcename + '"></div>';
            sourceDiv += '</div>';
            var loading;
			
            if ($('#selectors').find(option).length === 0){
                $('#selectors').append(sourceDiv).accordion();
                $('#selectors').accordion('destroy').accordion({
                    header: "h3", 
                    heightStyle: "fill",
                    collapsible: true
                });
            }
			
            loading = 'Retrieving properties list from the <b>' + option + '</b> SPARQL endpoint. Please wait...<img src="./images/ajax-loader.gif"/><br/><br/>';
            $(sourceid).html(loading);
                
            $.ajax({
                url: "get-properties",
                type: "GET",
                //processData: false,
                data: "service=" + dict[option] + "&prefixes=" + $("input[id=addressMap]").val(),
                error: function(responseText){
                    console.log(responseText);
                },
                success: function(responseText){
                    $(sourceid).html("");
                    var table;
                    table = "Add values to the relevant properties:<br/><i>Use <b>FILTER</b> if you do not know the exact value for the property.<br/>Use <b>AND</b> or <b>OR</b> to specify whether this property value pair will be conjuncted or disjuncted with the query term you specified above.<br/><br/></i>";
                    
                    var tokens = responseText.tokenize(",", " ", true); //converts the comma seperated string into an array
                    
                    table += "<form><table>";
                    $.each(tokens, function(val, p) {
                        table += "\
                            <tr>\n\
                               <td><a href='" + p + "'>" + getNameFromURI(p) + "</td>\n\
                               <td><input type='text' name=" + getNameFromURI(p) + " id=" + getNameFromURI(p) + " value='' class=\"inputTableUi\" /></td>\n\
                               <td><input type='checkBox' value='FILTER' />FILTER</td>\n\
                               <td><input type='radio' name='filter' id='and' value='AND' />AND</td>\n\
                               <td><input type='radio' name='filter' id='or' value='OR' />OR</input></td>\n\
                            </tr>";
                    });
                    
                    $(sourceid).html(table);
					
                    $('#selectors').accordion();
                    $('#selectors').accordion('destroy').accordion({
                        header: "h3", 
                        heightStyle: "fill",
                        collapsible: true
                    });
                }
            });
        }
    });

    $("#datasources :input:not(:checked)").each(function() {
        $('#selectors').find('#' + $(this).val()).remove();
    });
    
    $( "#selectors-resizer" ).resizable({
        minHeight: 450,
        minWidth: 820,
        resize: function() {
            $("#selectors").accordion("destroy");
        }
    });
}